<?php
header('Content-type:text/html;charset=utf-8');//设置编码格式
session_start(); // lcoalStorage, sessionStorage,cookie 同样的作用 后台用的 后台有 sessionStorage,cookie；
include_once 'dbconfig.php';
include_once 'common.php';
// 查询产品列表数据
if ($_GET) {
    if (isset($_GET['getType']) && $_GET['getType'] == 'gitList') {
        $search = $_GET['search'];
        $selecuSqlStr = 'SELECT * FROM  `product` WHERE  status =1 AND title LIKE \'%' . $search . '%\' LIMIT 0 ,' . $_GET['num'];
        chaxunshujuku($selecuSqlStr);
    } else if (isset($_GET['getType']) && $_GET['getType'] == 'salesPaiXu') {
        $search = $_GET['search'];
        $selecuSqlStr = 'SELECT * FROM  `product` WHERE status =1 AND title LIKE \'%' . $search . '%\'  ORDER BY  `product`.`sales` DESC LIMIT 0 ,  ' . $_GET['num'];
        chaxunshujuku($selecuSqlStr);
    } else if (isset($_GET['getType']) && $_GET['getType'] == 'evaluatePaiXu') {
        $search = $_GET['search'];
        $selecuSqlStr = 'SELECT * FROM  `product` WHERE status =1 AND title LIKE \'%' . $search . '%\'  ORDER BY  `product`.`evaluate` DESC LIMIT 0 ,  ' . $_GET['num'];
        chaxunshujuku($selecuSqlStr);
    }
}

if ($_POST) {
    if (isset($_SESSION['loginUserName'])) {
        $name = $_SESSION['loginUserName'];
    } else {
        $msg = ['msg' => '请先登录！', 'status' => 0];
        responseEvent($msg);
    }
    if (isset($_POST['postType']) && $_POST['postType'] == 'addCart') {
        $num = $_POST['num'];
        $proId = $_POST['id'];
        $color = $_POST['color'];
        $addTime = time() * 1000;
        $select = 'select * from shop_cart where username="' . $name . '" and status=1 and color="' . $color . '" and pro_id= ' . $proId;
        $res = getSelectArr($select);
        if (count($res) > 0) {
            $num = $res[0]['num'] + $num;
            $updateStr = 'update shop_cart set num=' . $num . ' where id=' . $res[0]['id'];
            if ($link->query($updateStr) === true) {
                $msg = ['msg' => '修改成功！', 'status' => 1, 'sid' => $res[0]['id']];
            } else {
                $msg = ['msg' => '修改失败！', 'status' => 0];
            }
            responseEvent($msg);
        }
        $insertStr = 'insert into shop_cart (username,pro_id,num,color,add_time) values("' . $name . '",' . $proId . ',' . $num . ',"' . $color . '","' . $addTime . '")';
        if ($link->query($insertStr) === true) {
            $select = 'select * from shop_cart where username="' . $name . '" and status=1 and color="' . $color . '" and pro_id= ' . $proId;
            $res = getSelectArr($select);
            $msg = ['msg' => '添加成功！', 'status' => 1, 'sid' => $res[0]['id']];
        } else {
            $msg = ['msg' => '添加失败！', 'status' => 0];
        }
        responseEvent($msg);
    }
}
